package com.spic.business.accident.analysisReport.controller;

import com.spic.business.accident.analysisReport.domain.AccidentAnalysisReport;
import com.spic.business.accident.analysisReport.service.IAccidentAnalysisReportService;
import com.spic.business.common.core.workflow.IWorkFlowService;
import com.spic.business.common.core.workflow.WorkFlowController;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.api.RemoteDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 事故分析报告Controller
 *
 * @author spic
 * @date 2021-11-30
 */
@RestController
@RequestMapping("/accident/analysisReport")
public class AccidentAnalysisReportController extends WorkFlowController {
    @Autowired
    private IAccidentAnalysisReportService accidentAnalysisReportService;
    @Autowired
    private RemoteDeptService remoteDeptService;

    /**
     * 查询事故分析报告列表
     */
    @PreAuthorize(hasPermi = "business/accident:analysisReport:list")
    @GetMapping("/list")
    public TableDataInfo list(AccidentAnalysisReport accidentAnalysisReport) {
        //获取事故报告单位及子部门
        if (null != accidentAnalysisReport.getReportDepartment()) {
            List<Long> reportDepartments = remoteDeptService.getDeptAndChildren(accidentAnalysisReport.getReportDepartment()).getData();
            accidentAnalysisReport.setReportDepartments(reportDepartments);
        }
        //获取事故发生单位及子部门
        if (StringUtils.isNotEmpty(accidentAnalysisReport.getAccidentDepartment())) {
            List<Long> accidentDepartments = remoteDeptService.getDeptAndChildren(Long.parseLong(accidentAnalysisReport.getAccidentDepartment())).getData();
            accidentAnalysisReport.setAccidentDepartments(accidentDepartments);
        }
        startPage();
        List<AccidentAnalysisReport> list = accidentAnalysisReportService.selectAccidentAnalysisReportList(accidentAnalysisReport);
        return getDataTable(list);
    }

    /**
     * 导出事故分析报告列表
     */
    @PreAuthorize(hasPermi = "business/accident:analysisReport:export")
    @Log(title = "事故分析报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccidentAnalysisReport accidentAnalysisReport) throws IOException {
        List<AccidentAnalysisReport> list = accidentAnalysisReportService.selectAccidentAnalysisReportList(accidentAnalysisReport);
        ExcelUtil<AccidentAnalysisReport> util = new ExcelUtil<AccidentAnalysisReport>(AccidentAnalysisReport.class);
        util.exportExcel(response, list, "事故分析报告数据");
    }

    /**
     * 获取事故分析报告详细信息
     */
    @PreAuthorize(hasPermi = "business/accident:analysisReport:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(accidentAnalysisReportService.selectAccidentAnalysisReportById(id));
    }

    /**
     * 新增事故分析报告
     */
    @PreAuthorize(hasPermi = "business/accident:analysisReport:add")
    @Log(title = "事故分析报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AccidentAnalysisReport accidentAnalysisReport) {
        if (StringUtils.isEmpty(accidentAnalysisReport.getAccidentInstantId())) {
            return AjaxResult.error("事故即时报告记录异常！");
        } else {
            AccidentAnalysisReport a = new AccidentAnalysisReport();
            a.setAccidentInstantId(accidentAnalysisReport.getAccidentInstantId());
            List<AccidentAnalysisReport> aList = accidentAnalysisReportService.selectAccidentAnalysisReportList(a);
            if (aList != null && aList.size() > 0) {
                return AjaxResult.error("当前事故即时报告记录已存在事故分析报告不能重复添加！");
            } else {
                return AjaxResult.success(accidentAnalysisReportService.insertAccidentAnalysisReport(accidentAnalysisReport));
            }
        }
    }

    /**
     * 修改事故分析报告
     */
    @PreAuthorize(hasPermi = "business/accident:analysisReport:edit")
    @Log(title = "事故分析报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccidentAnalysisReport accidentAnalysisReport) {
        return toAjax(accidentAnalysisReportService.updateAccidentAnalysisReport(accidentAnalysisReport));
    }

    /**
     * 删除事故分析报告
     */
    @PreAuthorize(hasPermi = "business/accident:analysisReport:remove")
    @Log(title = "事故分析报告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(accidentAnalysisReportService.deleteAccidentAnalysisReportByIds(ids));
    }

    /**
     * 获取本年份事故分析报告数量
     */
    @PreAuthorize(hasPermi = "business/accident:analysisReport:add")
    @GetMapping("countAnalysis/{year}")
    public TableDataInfo countAnalysis(@PathVariable String year) {
        startPage();
        List<AccidentAnalysisReport> list = accidentAnalysisReportService.countAnalysis(year);
        return getDataTable(list);
    }

    /**
     * 隐藏事故分析报告
     */
    @PreAuthorize(hasPermi = "business/accident:analysisReport:cover")
    @PutMapping("coverReport")
    public AjaxResult countAnalysis(@RequestBody AccidentAnalysisReport accidentAnalysisReport) {
        return toAjax(accidentAnalysisReportService.coverReport(accidentAnalysisReport));
    }

    @Override
    protected IWorkFlowService getService() {
        return accidentAnalysisReportService;
    }
}
